Sorts one list based on another list containing the desired indexes.
Use zip() and sorted() to combine and sort the two lists, based on the values of indexes. Use a list comprehension to get the first element of each pair from the result.
Code:
def sort_by_indexes(lst, indexes): return [val for _, val in sorted(zip(indexes, lst), key = lambda x: x[0])]
EXAMPLES
a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] b = [3, 2, 6, 4, 1, 5] sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
Sorts one list based on another list containing the desired indexes.
Use zip() and sorted() to combine and sort the two lists, based on the values of indexes. Use a list comprehension to get the first element of each pair from the result.
Code:
def sort_by_indexes(lst, indexes): return [val for _, val in sorted(zip(indexes, lst), key = lambda x: x[0])]
EXAMPLES
a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] b = [3, 2, 6, 4, 1, 5] sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
A leaked Telegram discussion by 50 so-called crypto influencers has exposed the extraordinary steps they take in order to profit on the back off unsuspecting defi investors. According to a leaked screenshot of the chat, an elaborate plan to defraud defi investors using the worthless “$Few” tokens had been hatched. $Few tokens would be airdropped to some of the influencers who in turn promoted these to unsuspecting followers on Twitter.
Traders also expressed uncertainty about the situation with China Evergrande, as the indebted property company has not provided clarification about a key interest payment.In economic news, the Commerce Department reported an unexpected increase in U.S. new home sales in August.Crude oil prices climbed Friday and front-month WTI oil futures contracts saw gains for a fifth straight week amid tighter supplies. West Texas Intermediate Crude oil futures for November rose $0.68 or 0.9 percent at 73.98 a barrel. WTI Crude futures gained 2.8 percent for the week.